﻿//定义一个条件编译指令
//#define debug

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_List : System.Web.UI.Page
{    
    public ConfigTable ThisTable = null;
    public string listType = string.Empty;
    public string listValue = string.Empty;

    public override string StyleSheetTheme
    {
        get
        {
            //return base.StyleSheetTheme;
            return ConfigGlobal.GetGlobal().Theme;
        }
        set
        {
            base.StyleSheetTheme = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.CacheControl = "no-cache";
        Response.Buffer = true;
        Response.Expires = -1000;
        ConfigGlobal.CheckLog();
        ConfigTable cTab =ConfigTable.GetThisPageConfigTable();
        ThisTable = cTab;

        this.output.Visible = cTab.AllowOutput;
        //添加grid字段，必须重新添加，因为模板列不添加的话不显示内容
        SetupGrid(cTab);

        listType = Request.QueryString["lt"];
        listValue = Request.QueryString["lv"];

        if (cTab.AllowMove && (!string.IsNullOrEmpty(cTab.TypeTableParamName)) && (!string.IsNullOrEmpty(Request.QueryString[cTab.TypeTableParamName])))
        {
            this.moveto.Visible = true;
        }
        else
        {
            this.moveto.Visible = false;
        }

        if (!Page.IsPostBack)
        {
            if (!cTab.AllowAdd)
            {
                this.addNewButton.Visible = false;
            }
            if (!cTab.AllowDel)
            {
                this.delAllButton.Visible = false;
            }
            //绑定Grid            
            cTab.BindGrid(this.SqlDataSource1, cTab.GetListBaseSql(), cTab.GetListWhereSql(listType,listValue), cTab.GetListOrderSql());
            ViewState["LastSql"] = this.SqlDataSource1.SelectCommand;

            #if debug
            Response.Write(this.SqlDataSource1.SelectCommand);
            #endif
        }
        else
        {
            //如果是postback要根据最后的sql重新绑定，因为重新设置了grid列
            ThisTable.BindGrid(this.SqlDataSource1, ViewState["LastSql"].ToString());
        }

        if (string.IsNullOrEmpty(cTab.ListViewName) || string.IsNullOrEmpty(cTab.ListViewXmlName))
        {
            if (cTab.Columns.Count(x => x.IsSearchShow) > 0)
            {
                cTab.SetUpSearchPanel(this.SearchFieldPanel);
            }
            else
            {
                this.SearchPanel.Visible = false;
            }
        }
        else
        {
            ConfigTable ViewTab = ConfigTable.GetConfigTable(cTab.ListViewName, cTab.ListViewXmlName);
            if (ViewTab.Columns.Count(x => x.IsSearchShow) > 0)
            {
                ViewTab.SetUpSearchPanel(this.SearchFieldPanel);
            }
            else
            {
                this.SearchPanel.Visible = false;
            }
        }
        this.delAllButton.Visible = cTab.AllowMultiSelect && cTab.AllowDel;
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sql=UIHelper.GridView1_Sorting(ViewState, sender, e);

        ConfigTable cTab = ThisTable;
                
        //绑定Grid
        cTab.BindGrid(this.SqlDataSource1, cTab.GetListBaseSql(), cTab.GetListWhereSql(listType, listValue), sql);
        ViewState["LastSql"] = this.SqlDataSource1.SelectCommand;

        #if debug
        Response.Write(this.SqlDataSource1.SelectCommand);
        #endif
    }

    #region 设置GridView
    private void SetupGrid(ConfigTable cTab)
    {        
        //设置基本字段
        cTab.SetupListGridFields(this.GridView1);
        cTab.SetOrderLink(this.GridView1);
        //设置查看
        cTab.SetGridShowLink(this.GridView1);
        //设置编辑
        cTab.SetGridEditLink(this.GridView1);
        //设置删除
        cTab.SetGridDelLink(this.GridView1);
        //设置分页
        this.GridView1.AllowPaging = cTab.AllowPageing;
        this.GridView1.PageSize = cTab.PageSize;        
    }
    #endregion
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {        
        GridView1.PageIndex = e.NewPageIndex;        
        ThisTable.BindGrid(this.SqlDataSource1, ViewState["LastSql"].ToString());
        e.Cancel = true;

        #if debug
        Response.Write(this.SqlDataSource1.SelectCommand);
        #endif
    }
    protected void SearchButton_Click(object sender, EventArgs e)
    {
        ConfigTable cTab = ThisTable;
        cTab.BindGrid(this.SqlDataSource1, cTab.GetListBaseSql(), cTab.GetListWhereSql(listType, listValue), cTab.GetListOrderSql());
        //Response.Write(cTab.GetListWhereSql());
        ViewState["LastSql"] = this.SqlDataSource1.SelectCommand;

        #if debug
        Response.Write(this.SqlDataSource1.SelectCommand);
        #endif
    }
    protected void SearchClearButton_Click(object sender, EventArgs e)
    {
        ConfigTable cTab = ThisTable;
        this.SearchFieldPanel.Controls.Clear();
        cTab.SetUpSearchPanel(this.SearchFieldPanel);        
    }
    protected void addNewButton_Click(object sender, EventArgs e)
    {
        ConfigTable cTab = ThisTable;
        string url = cTab.GetAddUrl();

        #if debug
        Response.Write(url);
        #else
        Response.Redirect(url);
        #endif        
    }
    protected void delAllButton_Click(object sender, EventArgs e)
    {
        ConfigTable cTab = ThisTable;
        string v = Request.Form["selectThis"];
        if (string.IsNullOrEmpty(v))
        {
            Helper.Alert("没有选择","nochooseAlert");
            return;
        }

        string url=cTab.DeleteUrl.Replace("{0}", v)+"&type=multy";

        #if debug
        Response.Write(url);
        #else
        Response.Redirect(url);
        #endif        
    }
    protected void moveto_Click(object sender, EventArgs e)
    {
        ConfigTable cTab = ThisTable;
        string v = Request.Form["selectThis"];
        if (string.IsNullOrEmpty(v))
        {
            Helper.Alert("没有选择", "nochooseAlert");
            return;
        }

        string movetourl = "moveto.aspx?tab={0}&xml={1}&typetab={2}&col="+cTab.FkFiledName+"&ids="+v;
        string[] param = { "tab:q","xml:q","typetab:q" };

        //string url = "moveto.aspx?typeTab=" + Request.QueryString["typetab"] + "&ids=" + v+"&tab="+cTab.TableName+"&col="+cTab.FkFiledName;
        string url = ConfigTable.SetupLink(movetourl, ref param);

        #if debug
                Response.Write(url);
        #else
                Response.Redirect(url);
        #endif        
    }
    protected void output_Click(object sender, EventArgs e)
    {
        listType = Request.QueryString["lt"];
        listValue = Request.QueryString["lv"];

        ExcelManager.Output(this.ThisTable, listType, listType);
    }
}
